#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#define SERVER_PORT 8080
#define MAX 80

/*
struct scokaddr_in{
	sa_family_t	 sin_family; //address family:AF_INET
	int_port_t	sin_port;//port in network byte order
	struct in_addr	sin_addr; // internet address
};

struct in_addr{
	uint32_t s_addr; //address in network byte order
}
*/
int main(int argc, char **argv)
{
	struct sockaddr_in serveradd, clientadd;
	int sockfd,accept_info,i,n;
	socklen_t clientadd_len;
	char buf[MAX];
	char str[INET_ADDRSTRLEN];
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
		printf("server socket fail\n");
	memset(&serveradd,0,sizeof(serveradd));
	serveradd.sin_family=AF_INET;
	serveradd.sin_port=htons(SERVER_PORT);
	//serveradd.sin_addr.s_addr=inet_addr("192.168.20.23");;
	serveradd.sin_addr.s_addr=htonl(INADDR_ANY);
	if(bind(sockfd,(struct sockaddr *)&serveradd,sizeof(serveradd))<0)
		perror("bind fail");
	listen(sockfd,20);
	printf("waiting for client connect.....\n");
	clientadd_len=sizeof(clientadd);
	while(1)
	{
		accept_info=accept(sockfd,(struct sockaddr *)&clientadd,&clientadd_len);
		n=read(accept_info,buf,MAX);
		printf("from client(ip:%s port:%d)information as follow:\n",inet_ntop(AF_INET,&clientadd.sin_addr,str,sizeof(str)),ntohs(clientadd.sin_port));
		printf("%s\n",buf);
		for(i=0;i<n;i++)
			buf[i]=toupper(buf[i]);
		printf("n is %d\n",n);
		write(accept_info,buf,n);
		close(accept_info);
	}
	return 0;
}
